﻿using System.Net.Http.Headers;

namespace llm_project.Infrastructure.Helpers
{
    public static class HttpClientSingleton
    {
        // 静态单例实例
        private static readonly HttpClient _httpClient;

        // 静态构造函数，确保只执行一次
        static HttpClientSingleton()
        {
            _httpClient = new HttpClient();
            // 设置默认超时时间（30秒）
            _httpClient.Timeout = TimeSpan.FromSeconds(30);
            // 默认Accept头部
            _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }

        /// <summary>
        /// 获取全局HttpClient实例
        /// </summary>
        public static HttpClient GetClient() => _httpClient;

        /// <summary>
        /// 为特定API配置头部（如认证信息）
        /// </summary>
        public static void ConfigureApiHeaders(string appKey)
        {
            // 移除已存在的相同头部，避免重复
            if (_httpClient.DefaultRequestHeaders.Contains("Authorization"))
                _httpClient.DefaultRequestHeaders.Remove("Authorization");

            _httpClient.DefaultRequestHeaders.Add("Authorization", appKey);
        }
    }
}
